package com.example.administrator.demos;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by Administrator on 2017/3/20 0020.
 */
public class FruitAdapter extends RecyclerView.Adapter {
    private List<Fruit> mFruitlist;

    static class ViewHolder extends RecyclerView.ViewHolder{
        View fruitView;
        ImageView fruitImage;
        TextView fruitName;

        public ViewHolder(View view){
            super(view);
            fruitView = view;
            fruitName = (TextView) view.findViewById(R.id.fruit_name);
            fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
            Log.d("aaaaa","我在代码中添加了一点无关紧要的东西，看能不能识别出来");
        }
    }

    public FruitAdapter(List<Fruit> fruitlist){
        mFruitlist = fruitlist;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.fruit_item, null);
        final ViewHolder holder = new ViewHolder(view);
        //两个点击事件
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Toast.makeText(v.getContext(),
                        "我的位置是:  "+position+"\n"+"我的名字是：  "+
                        mFruitlist.get(position).getName()
                        +"\n"+"我的类型是:  view"
                        ,Toast.LENGTH_SHORT).show();
            }
        });

        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Toast.makeText(v.getContext(),
                        "我的位置是:  "+position+"\n"+"我的名字是：  "+
                                mFruitlist.get(position).getName()
                                +"\n"+"我的类型是:  image"
                        ,Toast.LENGTH_SHORT).show();
            }
        });


        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Fruit mfruit = mFruitlist.get(position);
        ViewHolder mholder = (ViewHolder) holder;
        mholder.fruitImage.setImageResource(mfruit.getImageId());
        mholder.fruitName.setText(mfruit.getName());
    }

    @Override
    public int getItemCount() {
        return mFruitlist.size();
    }
}
